package models

import (
	"regexp"
	"strconv"
	"strings"

	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
)

//全局连接实例
var (
	db orm.Ormer
)

//由于model这个名字叫 UserInfo 那么操作的表其实 user_info
type MovieInfo struct {
	Id                   int64
	Movie_id             int64
	Movie_name           string
	Movie_pic            string
	Movie_director       string
	Movie_writer         string
	Movie_country        string
	Movie_language       string
	Movie_main_character string
	Movie_type           string
	Movie_on_time        string
	Movie_span           string
	Movie_grade          string
	Remark               string
	Create_time          string
}

func init() {
	// 获取配置信息
	config := beego.AppConfig.String
	dsn := config("dsn")
	drivername := config("drivername")
	runmode := config("runmode")
	var debug bool
	if runmode == "dev" {
		debug = false
	} else {
		debug = true
	}
	orm.Debug = debug //是否开启调试模式，调试模式开启会打印sql语句
	orm.RegisterDataBase("default", drivername, dsn, 30)
	//orm.RegisterDataBase("default", "mysql", "root:0000@tcp(127.0.0.1:3306)/test?charset=utf8", 30)
	orm.RegisterModel(new(MovieInfo))
	orm.RunSyncdb("default", false, true)
	db = orm.NewOrm()
}

// AddMovice
func AddMovie(movie_info *MovieInfo) (int, error) {
	id, err := db.Insert(movie_info)
	return int(id), err
}

func GetMovieDirector(movieHtml string) string {
	if movieHtml == "" {
		return ""
	}
	reg := regexp.MustCompile(`<a.*?rel="v:directedBy">(.*)</a>`)
	result := reg.FindAllStringSubmatch(movieHtml, -1)
	if len(result) == 0 {
		return ""
	}
	return string(result[0][1])
}
func GetMovieId(url string) int64 {
	reg := regexp.MustCompile(`https://movie.douban.com/subject/(.*)`)
	result := reg.FindAllStringSubmatch(url, -1)
	movie_id, _ := strconv.ParseInt(result[0][1], 10, 64)
	return movie_id
}

func GetMovieName(movieHtml string) string {
	if movieHtml == "" {
		return ""
	}
	reg := regexp.MustCompile(`<span property="v:itemreviewed">(.*?)</span>`)
	result := reg.FindAllStringSubmatch(movieHtml, -1)

	if len(result) == 0 {
		return ""
	}
	return string(result[0][1])
}

func GetMovieMainCharacters(movieHtml string) string {
	reg := regexp.MustCompile(`<a.*?rel="v:starring">(.*?)</a>`)
	result := reg.FindAllStringSubmatch(movieHtml, -1)
	mainCharacters := ""
	for _, v := range result {
		mainCharacters += v[1] + "/"
	}
	if len(result) == 0 {
		return ""
	}
	return strings.Trim(mainCharacters, "/")
}

func GetMoviePic(movieHtml string) string {
	reg := regexp.MustCompile(`<img src="(.*?)".*?rel="v:image" />`)
	result := reg.FindAllStringSubmatch(movieHtml, -1)
	if len(result) == 0 {
		return ""
	}
	return string(result[0][1])

}

func GetMovieWriter(movieHtml string) string {
	reg := regexp.MustCompile(`<span ><span class='pl'>编剧</span>: <span class='attrs'><a href=".*?">(.*?)</a></span></span>`)
	result := reg.FindAllStringSubmatch(movieHtml, -1)
	if len(result) == 0 {
		return ""
	}
	return string(result[0][1])
}

func GetMovieCountry(movieHtml string) string {
	reg := regexp.MustCompile(`<span class="pl">制片国家/地区:</span>(.*?)<br/>`)
	result := reg.FindAllStringSubmatch(movieHtml, -1)
	if len(result) == 0 {
		return ""
	}
	return string(result[0][1])
}

func GetMovieLanguage(movieHtml string) string {
	reg := regexp.MustCompile(`<span class="pl">语言:</span>(.*?)<br/>`)
	result := reg.FindAllStringSubmatch(movieHtml, -1)
	if len(result) == 0 {
		return ""
	}
	return string(result[0][1])
}

func GetMovieType(movieHtml string) string {
	reg := regexp.MustCompile(`<span property="v:genre">(.*?)</span>`)
	result := reg.FindAllStringSubmatch(movieHtml, -1)
	if len(result) == 0 {
		return ""
	}
	mainCharacters := ""
	for _, v := range result {
		mainCharacters += v[1] + "/"
	}
	return mainCharacters
}

func GetMovieOnTime(movieHtml string) string {
	reg := regexp.MustCompile(`<span property="v:initialReleaseDate".*?>(.*?)</span>`)
	result := reg.FindAllStringSubmatch(movieHtml, -1)
	if len(result) == 0 {
		return ""
	}
	mainCharacters := ""
	for _, v := range result {
		mainCharacters += v[1] + "/"
	}
	return mainCharacters
}
func GetMovieSpan(movieHtml string) string {
	reg := regexp.MustCompile(`<span property="v:runtime".*?>(.*?)</span>`)
	result := reg.FindAllStringSubmatch(movieHtml, -1)
	if len(result) == 0 {
		return ""
	}
	return string(result[0][1])
}
func GetMovieGrade(movieHtml string) string {
	reg := regexp.MustCompile(`<strong class="ll rating_num" property="v:average">(.*?)</strong>`)
	result := reg.FindAllStringSubmatch(movieHtml, -1)
	if len(result) == 0 {
		return ""
	}
	return string(result[0][1])
}

func GetMovieUrls(movieHtml string) []string {
	reg := regexp.MustCompile(`<a.*?href="(https://movie.douban.com/subject/.*?)/.*?"`)
	result := reg.FindAllStringSubmatch(movieHtml, -1)

	var movieSets []string
	for _, v := range result {
		movieSets = append(movieSets, v[1])
	}
	return movieSets
}
